package com.x.xiaoshuo.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;


public class PageBackgoundColorDrawable extends Drawable {
    private static final float DEFAULT_WIDTH = 45f;

    private static final float DEFAULT_HEIGHT = 45f;

    private float mWidth;

    private float mHeight;

    private Paint mSelectPaint;

    private Paint mPaint;

    private int color = Color.parseColor("#fffae6");

    private boolean isSelect = false;

    private Drawable drawable;


    public PageBackgoundColorDrawable(Context context) {
        mWidth = dp2px(context, DEFAULT_WIDTH);
        mHeight = dp2px(context, DEFAULT_HEIGHT);
        mPaint = new Paint();
        mPaint.setColor(color);
        mPaint.setAntiAlias(true);

        mSelectPaint = new Paint();
        mSelectPaint.setColor(Color.parseColor("#e1544d"));
        mSelectPaint.setAntiAlias(true);
    }

    public void setmWidth(float mWidth) {
        this.mWidth = mWidth;
    }

    public void setmHeight(float mHeight) {
        this.mHeight = mHeight;
    }

    public void setColor(String color) {
        this.color = Color.parseColor(color);
        mPaint.setColor(Color.parseColor(color));
        invalidateSelf();
    }

    public void setColor(int color) {
        this.color = color;
        mPaint.setColor(color);
        invalidateSelf();
    }

    public void setSelect(boolean select) {
        isSelect = select;
        invalidateSelf();
    }

    public void setDrawable(Drawable drawable) {
        this.drawable = drawable;
        Bitmap bitmap = drawableToBitmap(drawable);
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        mPaint.setShader(bitmapShader);
        invalidateSelf();
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        int span = 0;
        if (isSelect) {
            span = 5;
            canvas.drawCircle(mWidth / 2, mHeight / 2, mHeight / 2, mSelectPaint);
        }
        canvas.drawCircle(mWidth / 2, mHeight / 2, mHeight / 2 - span, mPaint);
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }

    public float dp2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return dpValue * scale;
    }

    private Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            return bd.getBitmap();
        }
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);
        return bitmap;
    }
}
